package com.example.springframeworklearning.config;

import com.example.springframeworklearning.converter.MapToOrderConverter;
import com.example.springframeworklearning.converter.StringOrderStatusConverterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ConversionServiceFactoryBean;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;

/**
 * ConversionService 是 Converter ConverterFactory ConditionalGenericConverter 等等的一个门面 facade
 * 如果配置了 ConversionService 就不要再重复往 ioc 里面配置 上述 Converter 了
 *
 * @author luo
 * @since 2022/9/20 10:52
 */
@Configuration
public class ConversionServiceConfig {


//    @Bean
    public ConversionServiceFactoryBean converterService() {
        /**
         *ConversionServiceFactoryBean 中的默认实现是 DefaultConversionService
         * 而 DefaultConversionService 中已经注册了大多数常用的 converter
         */
        ConversionServiceFactoryBean result = new ConversionServiceFactoryBean();

        HashSet<Object> converterSet = new HashSet<>(
                Arrays.asList(
//                注册 converter
                        new MapToOrderConverter(),
//                注册 converterFactory
                        stringOrderStatusConverterFactory()
                )
        );
//        把所有的 converter 注册到 ConversionServiceFactoryBean 中
        result.setConverters(converterSet);

        return result;
    }

    @Bean
    public StringOrderStatusConverterFactory stringOrderStatusConverterFactory() {
        return new StringOrderStatusConverterFactory();

    }
}
